package com.itheima.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.common.constants.SystemConstants;
import com.itheima.common.vo.PageResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @program: itheima-leadnews
 * @description: 实现类
 * @author: pw
 * @create: 2022-08-09 23:09
 **/
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

   @Autowired
   private RedisTemplate redisTemplate;

    @Override
    public PageResultVo<AdChannel> pageList(ChannelPageRequestDto pageRequestDto) {
//        构建查询规则
        LambdaQueryWrapper<AdChannel> qw = new LambdaQueryWrapper<>();
        IPage<AdChannel> page = new Page<>();
        page.setSize(pageRequestDto.getSize());
        page.setPages(pageRequestDto.getPage());
        qw.like(!StringUtils.isEmpty(pageRequestDto.getName()), AdChannel::getName, pageRequestDto.getName())
                .eq(!StringUtils.isEmpty(pageRequestDto.getStatus()), AdChannel::getStatus, pageRequestDto.getStatus());
        page(page, qw);
        return PageResultVo.pageResult(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());


//        return PageResultVo.pageResult(page.getCurrent(),page.getSize(),page.getTotal(),page.getRecords());
    }
//redis缓存

//    @Override
//    public List<AdChannel> cacheList() {
//       String re=(String)redisTemplate.opsForValue().get(SystemConstants.REDISKEY_CHANNELLIST);
//        List<AdChannel> adChannel=null;
//        if (re==null){
//            adChannel = list();
//        }
//        adChannel= JSON.parseArray(re,AdChannel.class);
//
//        return adChannel;
//    }
}
